<script>
    var sendVerifier = require('mobile-handle').sendSmsVerify;
    var verifier = require('mobile-handle').smsVerify;

    $(function () {

        var form = new Vue({
            el:'#article',
            data:{
                phone:window.phone,
                smsVerified:false,
                delay:0,
            },
            mounted:function(){
            },
            methods:{
                sendSmsVerify:_.throttle(function(event){
                    if (this.delay > 0){
                        return;
                    }

                    var self = this;
                    var that = event.target;
                    sendVerifier(this.phone,function(ret,message){
                        if (ret){
                            self.delay = 60;
                            $(that).addClass('gray').text('发送验证码('+self.delay+')');
                            var delayer = setInterval(function () {
                                self.delay--;
                                $(that).text('发送验证码('+self.delay+')');
                                if (self.delay <= 0) {
                                    clearInterval(delayer);
                                    self.delay = 0;
                                    $(that).text('发送验证码').removeClass('gray');
                                }
                            },1000);
                        }
                        alert(message);
                    });
                },3000),
                smsVerify:_.throttle(function(event){
                    if (!sendVerifyForm_validator.element( "[name=verifyCode]" )){
                        return;
                    }

                    var self = this;
                    var that = event.target;
                    verifier(this.phone,$("[name=verifyCode]").val(),function(ret,message){
                        if (ret){
                            self.smsVerified = true;
                        }
                        alert(message);
                    });
                },1500),
            }
        });

        var sendVerifyForm_validator = $("#sendVerifyForm").validate({
            rules: {
                verifyCode:{
                    required:true,
                }
            },
            messages: {
                verifyCode:{
                    required:'请输入验证码',
                }
            },
        });

        var validator = $("#form").validate({
            rules: {
                wallet_password:{
                    required:true,
                    minlength:6,
                    maxlength:20,
                    numberAndLetter:true,
                },
                wallet_password_confirm:{
                    equalTo:'[name=wallet_password]',
                }
            },
            messages: {
                wallet_password:{
                    required:'请输入密码',
                },
                wallet_password_confirm:{
                    equalTo:'两次密码输入不一致',
                }
            },
            submitHandler: function(form) {
                form.submit();
            },
        });
    });
</script>